// This is our javascript, which will pop up our message
// in an alert box.

//function hello() {
//	alert("Hello, world!");
//}



//function textHyperlink(e) {
//    if (e.shiftKey) {
//        var selectedTxt = content.getSelection().getRangeAt(0);
//        documentClickHandler();
//    }
//}

//function documentClickHandler() {
//    var selectedTxt = content.getSelection().getRangeAt(0);
//    if (isUrl(selectedTxt)) {
//        var newWindow = content.open(selectedTxt, '_blank');
//        return false;
//    }
//}

//function openUrl(urlStr) {
//    var newWindow = content.open(selectedTxt, '_blank');
//}


//function isUrl(s) {
//    var regexp = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
//    return regexp.test(s);
//}


//document.addEventListener("click", textHyperlink, false);


function LoadUrlHyperlinks(aEvent) {

    if (aEvent.originalTarget.nodeName == "#document") {

        
        
        //var appcontent = document.getElementById("appcontent");
        //alert("foo bar");
        var d = window.content.document;
        var o;
        o =  [o || d.documentElement || d.body];
        // alert(d);

        //var r = /http(s)?:\/\/([\w-]+\.)+[\w-]+(\/[\w- .\/?%&=]*)?/gi;
        var r = /(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/gi;
        h = d.createElement('span'),
        //alert(h);    

        i = 0;
        var j;
        var k;
        var l;
        var m;
        n = 0;
        var t;
        h.style.color = '#000';
        h.style.backgroundColor = 'Lime';
        do {
            m = o[i];
            //alert(m);
            if (m.nodeType === 3) {
                //alert(m.nodeType);
                r.lastIndex = 0;
                l = r.exec(m.nodeValue);
                if (l !== null) {
                    k = l[0].length;
                    if (r.lastIndex > k) {
                        m.splitText(r.lastIndex - k);
                        m = m.nextSibling;
                    }
                    if (m.nodeValue.length > k) {
                        m.splitText(k);
                        o[i++] = m.nextSibling;
                    }
                    t = d.createElement('span');
                    t.style.textDecoration = "underline"
                    t.style.cursor = 'pointer';
                    t.style.color = 'Blue';
                    t.addEventListener('click', function() {
                        content.open(this.innerHTML, '_blank')
                    }, false);
                    t.appendChild(d.createTextNode(l[0])); n++;
                    m.parentNode.replaceChild(t, m);
                }
            } else {
                j = m.childNodes.length;
                while (j) { o[i++] = m.childNodes.item(--j); }
            }
        } while (i--);
        return n;
    }
}

window.addEventListener("load", function() { UrlHyperlinkInit(); }, false);
function UrlHyperlinkInit() {
    var appcontent = document.getElementById("appcontent");
    appcontent.addEventListener("DOMContentLoaded", LoadUrlHyperlinks, true);
}
